import Vue from 'vue'
import VueRouter from 'vue-router'

// 安装插件
Vue.use(VueRouter)

// 懒路由写法
const Login = () => import('../components/common/Login/Login')
const Home = () => import('../components/content/Home/Home')
const Welcome = () => import('../views/welcome/Welcome')
const User = () => import('../views/user/User')
const Rights = () => import('../views/power/Rights')
const Roles = () => import('../views/power/Roles')
const Cate = () => import('../views/goods/Cate')
const Params = () => import('../views/goods/Params')
const GoodsList = () => import('../views/goods/List')
const AddGoods = () => import('../views/goods/AddGoods')
const Order = () => import('../views/order/Order')
const Report = () => import('../views/report/Report')

  const routes = [
    {
      path: '/',
      redirect: '/Login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: User
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/categories',
          component: Cate
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/goods',
          component: GoodsList
        },
        {
          path: '/goods/add',
          component: AddGoods
        },
        {
          path: '/orders',
          component: Order
        },
        {
          path: '/reports',
          component: Report
        }
      ]
    }
]
const router = new VueRouter({
  routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to是将要访问的路径
  // from代表从哪个路径跳转出来
  //  next是一个函数，标识放行
  //  next()放行  next('/login')强制跳转
  if (to.path === '/login') return next()
    // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
